package cn.damai.search.baseService.ipml;

import cn.damai.common.EmptyUtils;
import cn.damai.common.MD5;
import cn.damai.search.baseService.BaseUserService;
import cn.damai.model.User;
import cn.damai.search.mapper.UserMapper;
import cn.damai.util.DubboUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Component
@Service(version = DubboUtil.VRESION,group = DubboUtil.GROUP)
public class BaseUserServiceIpml implements BaseUserService {

    private String tokenPrefix = "token";
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(@RequestBody Long id){
        return userMapper.getUserById(id);
    }

    @Override
    public List<User>	getUserListByMap(@RequestBody Map<String,Object> param){
        return userMapper.getUserListByMap(param);
    }

    @Override
    public Integer getUserCountByMap(@RequestBody Map<String,Object> param){
        return userMapper.getUserCountByMap(param);
    }

    @Override
    public Integer qdtxAddUser(@RequestBody User user){
        user.setCreatedTime(new Date());
        return userMapper.insertUser(user);
    }

    @Override
    public Integer qdtxModifyUser(@RequestBody User user){
        user.setUpdatedTime(new Date());
        return userMapper.updateUser(user);
    }

    @Override
    public User checkloginByPassword(@RequestBody User user) {
       return userMapper.checkLoginByPassword(user.getPhone(),user.getPassword());
    }

    //Token 管理服务
    @Override
    public String generateToken(@RequestBody User user) {
        StringBuilder sb = new StringBuilder();
        sb.append(tokenPrefix);
        sb.append("pc-"+"-");
        String info = MD5.getMd5(EmptyUtils.isEmpty(user.getPhone()) ? user.getWxUserId() : user.getPhone(),32);
        sb.append(info+"-");
        sb.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+"-");
        sb.append(UUID.randomUUID().toString().substring(0,6));
        return sb.toString();
    }
}
